package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.GjmyServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "gjmy")
public class GjmyAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(GjmyAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());

		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo =orderBean.getIds(); 
		orderBean.setSendTime(new Date());
		log.info("between- 国际漫游流量订单GjmyService.order() 订购请求参数 productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = GjmyServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code =  json.getString("resultCode");
		// 00表示订购中
		if (("00".equals(code))) {
		
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("resultDesc"));
		} else {
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("resultDesc"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();	
		String cstmOrderNo = orderBean.getIds();
		log.info("between -国际漫游流量状态GjmyService.orderStatus() 查询请求参数cstmOrderNo:"+cstmOrderNo);
		String result = GjmyServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("resultCode");

		
		if (("00".equals(code))) {
			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
			orderBean.setMessage(json.getString("resultDesc"));
		} else if (("02".equals(code))) {
			
			orderBean.setErrorMsg(json.getString("resultDesc"));
			orderBean.setStatus("1");
			orderBean.setMessage(json.getString("resultDesc"));
		} else {
			
			orderBean.setErrorMsg(json.getString("resultDesc"));
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("resultDesc"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
